<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\store\StoreAuthUserModel;
use app\model\store\StoreModel;
use think\Request;

/**
 * 商家控制器
 */
class Store extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new StoreModel();

    }

    /**
     * 分页数据
     *
     * @param string $keyword
     * @param string $store_status
     * @param string $check_status
     * @param integer $page
     * @param integer $limit
     * @return void
     */
    public function getPage($keyword = '', $store_status = '', $check_status, $page = 1, $limit = 10)
    {
        $data = $this->model->getPageData($keyword, $store_status, $check_status, $page, $limit);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 获取全部店铺
     * @return \think\response\Json
     */
    public function getAll()
    {
        $data = $this->model->getAll([], "id,store_name");
        return self::returnMsg(1, $data
        );
    }

    /**
     * 获取详情
     *
     * @param [type] $id
     * @return void
     */
    public function getInfo($id)
    {
        $data = $this->model->getInfo($id, '*', ['grade', 'user']);
        $data->append(['store_type_text', 'store_status_text', 'check_status_text', 'is_own_shop_text']);
        $data = json_decode(json_encode($data), true);
        if (!empty($data['card_photo'])) {
            if (strpos($data['card_photo'], ',') > 0) {
                $card_photo = explode(',', $data['card_photo']);
            } else {
                $card_photo[] = $data['card_photo'];
            }
            $data['card_photo'] = $card_photo;
        }
        if (empty($data['store_top'])) {
            $data['top_info'] = "暂无邀请人";
        } else {
            $store = $this->model->getInfo($data['store_top'], 'store_name,contact_phone');
            $data['top_info'] = $store['store_name'];
        }
        if (!empty($data)) {
            return self::returnMsg(1, $data);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 保存
     *
     * @param Request $request
     * @return void
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'user_id' => $request->param('user_id'),
            'grade_id' => $request->param('grade_id'),
            'store_name' => $request->param('store_name'),
            'store_type' => $request->param('store_type'),
            'store_deposit' => $request->param('store_deposit'),
            'store_start_time' => $request->param('store_start_time'),
            'store_end_time' => $request->param('store_end_time'),
            'store_label' => $request->param('store_label'),
            'store_keywords' => $request->param('store_keywords'),
            'sort' => $request->param('sort'),
            'store_workingtime' => $request->param('store_workingtime'),
            'is_own_shop' => $request->param('is_own_shop'),
            'store_top' => $request->param('store_top'),
            'popularize_code' => $request->param('popularize_code'),
            'store_desc' => $request->param('store_desc'),
            'store_scope' => $request->param('store_scope'),
            'real_name' => $request->param('real_name'),
            'card_photo' => $request->param('card_photo'),
            'contact_phone' => $request->param('contact_phone'),
        ];
        $formData['store_start_time'] = strtotime($formData['store_start_time']);
        $formData['store_end_time'] = strtotime($formData['store_end_time']);
        if (empty($formData['id'])) {
            $validate = 'app\validate\StoreValidate.add';
        } else {
            $validate = 'app\validate\StoreValidate';
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'StoreModel', '店铺管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'store');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }

    /**
     * 上线状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'store_status' => $request->param('status'),
        ];
        if (is_array($formData['id'])) {
            $list = [];
            foreach ($formData['id'] as $key => $value) {
                $list[] = ['id' => $value, 'store_status' => $formData['store_status']];
            }
            $ret = $this->model->saveAll($list);
            if ($ret) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        } else {
            $validate = 'app\validate\StoreValidate.upstorestatus';
            $result = $this->validate($formData, $validate);
            if (true !== $result) {
                return self::returnMsg(991, [], $result);
            } else {
                $ret = $this->model->saveData($formData, false, false);
                if (false !== $ret['flag']) {
                    return self::returnMsg(1);
                } else {
                    return self::returnMsg(0);
                }
            }
        }
    }

    /**
     * 修改审核状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onCheckStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'contact_phone' => $request->param('contact_phone'),
            'check_status' => $request->param('check_status'),
            'is_own_shop' => $request->param('is_own_shop'),
            'store_check_time' => date('Y-m-d H:i:s', time()),
            'store_check_info' => $request->param('store_check_info'),
        ];
        if (empty($formData['contact_phone'])) {
            return self::returnMsg(991, [], "参数有误");
        }
        $validate = 'app\validate\StoreValidate.upcheckstatus';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData);
            if (false !== $ret['flag']) {
                if (intval($formData['check_status']) == 1) {
                    $store_auth_user_model = new StoreAuthUserModel();
                    $ret = $store_auth_user_model->addStoreManage($formData['contact_phone'], $formData['id']);
                }
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }
}